#include "addyiping.h"
#include<qdebug.h>
#include<qfile.h>

addyiping::addyiping(QObject *parent) : QObject(parent)
{
    //处理完成事件
    QObject::connect(&process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
                             [=] (int exitCode, QProcess::ExitStatus exitStatus) {

                           qDebug()<< "Exit code: " << exitCode
                           << "Exit status: " << exitStatus;

                           emit signal_addyinping_over();

            });

     //读取控制台输出
     connect(&process , SIGNAL(readyReadStandardOutput()) , this , SLOT(on_readoutput()));
     //读取控制台输出错误
     connect(&process , SIGNAL(readyReadStandardError()) , this , SLOT(on_readerror()));

     //开始运行事件
     // connect(&process,SIGNAL(started()),SLOT(handle_start()));


}

void addyiping::Addyinping(QString shipingfile,QString yinpingfile,QString outfile)
{

    //ffmpeg -i H:\jingting.mp4 -i H:\jingting.mp3 H:\new1.mp4
    qDebug()<<"void addyiping::Addyinping(QString shipingfile,QString yinpingfile,QString outfile)";

    //ffmpeg -i video_no_audio.mp4 -stream_loop -1 -i 世界这么大.wav -ss 0 -t 30 -y out.mp4

    QString program="ffmpeg";
    QStringList argu;
    argu.append("-y");
    argu.append("-i");
    argu.append(shipingfile);

    argu.append("-i");
    argu.append(yinpingfile);

    argu.append(outfile);

    process.start(program,argu);
    //process.waitForFinished(-1);


}

void addyiping::video_stitch(QString shiping_1, QString shiping_2, QString outfile)
{

    //QString argu_1=QString("movie=%1, scale=512:288 [v1] ; amovie=%1 [a1] ;movie=%2, scale=512:288 [v2] ; amovie=%2 [a2] ; [v1] [v2] concat [outv] ; [a1] [a2] concat=v=0:a=1 [outa]; -map '[outv]' -map '[outa]' %3").arg(shiping_1).arg(shiping_2).arg(outfile);

    //QStringList argu=argu_1.split(" ");//这样吧文件名也分开了
    QString program="ffmpeg";
    QStringList argu;
    argu.append(QString("movie=%1, scale=512:288 [v1] ; amovie=%1 [a1] ;").arg(shiping_1));
    argu.append(QString("scale=512:288 [v2] ; amovie=%2 [a2] ; [v1] [v2] concat [outv] ; [a1] [a2] concat=v=0:a=1 [outa]; -map '[outv]' -map '[outa]' ;").arg(shiping_2));
    argu.append(QString("%1").arg(outfile));
    process.start(program,argu);

}


void addyiping::on_readoutput()
{
   QString str(process.readAllStandardOutput().data());   //将输出信息读取到编辑框
   qDebug()<<str;
   emit signal_outtext(str);
}
void addyiping::on_readerror()
{
    QString str (process.readAllStandardError().data());
    qDebug()<<   str ;//弹出信息框提示错误信息
    emit signal_outtext(str);
}
void addyiping::handle_start()
{
    qDebug()<<"handle start";
}

